package com.example.service_statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.service_statistics.client.MemberClient;
import com.example.service_statistics.entity.StatisticsDaily;
import com.example.service_statistics.mapper.StatisticsDailyMapper;
import com.example.service_statistics.service.StatisticsDailyService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:tjf
 * @Description:
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily>
                                                        implements StatisticsDailyService {
    @Resource
    private MemberClient memberClient;
    @Resource
    private StatisticsDailyMapper statisticsMapper;

    /**
     * 保存统计数据
     * @param dayStr
     * @return
     */
    @Transactional
    public StatisticsDaily saveStatisticsData(String dayStr) {
        // 查询注册数
        int registerNum = memberClient.countRegisterMember(dayStr);

        // 模拟数据
        int loginNum = RandomUtils.nextInt(100, 1000);
        int videoViewNum = RandomUtils.nextInt(100, 1000);
        int courseNum = RandomUtils.nextInt(0, 4) -1;

        // 封装数据
        StatisticsDaily statisticsData = new StatisticsDaily();
        statisticsData.setDateCalculated(dayStr)
                        .setRegisterNum(registerNum + RandomUtils.nextInt(100, 500))
                        .setLoginNum(loginNum)
                        .setCourseNum(courseNum)
                        .setVideoViewNum(videoViewNum);

        // 保存数据
        LambdaUpdateWrapper<StatisticsDaily> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(StatisticsDaily::getDateCalculated, dayStr);
        if (statisticsMapper.update(statisticsData, wrapper) <= 0){// 当更新结果返回0时，表示没有记录
            statisticsMapper.insert(statisticsData);
        }
        return statisticsData;
    }

    /**
     * 查询指定时间段的统计日数据
     * @param begin
     * @param end
     * @return
     */
    @Override
    public Map<String, Object> queryStatisticsData(String begin, String end) {
        LambdaQueryWrapper<StatisticsDaily> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(StatisticsDaily::getDateCalculated, begin, end)
                .orderByAsc(StatisticsDaily::getDateCalculated);
        // 选择查询字段
        wrapper.select(StatisticsDaily::getDateCalculated,
                StatisticsDaily::getRegisterNum, StatisticsDaily::getLoginNum,
                StatisticsDaily::getVideoViewNum, StatisticsDaily::getCourseNum);
        List<String> dateCalculateList = new ArrayList<>();
        List<Integer> registerNumList = new ArrayList<>();
        List<Integer> loginNumList = new ArrayList<>();
        List<Integer> videoViewNumList = new ArrayList<>();
        List<Integer> courseNumList = new ArrayList<>();

        // 封装数据
        List<StatisticsDaily> statisticsDailyList = statisticsMapper.selectList(wrapper);
        if (statisticsDailyList != null && statisticsDailyList.size() > 0){
            for (StatisticsDaily statisticsDaily : statisticsDailyList) {
                dateCalculateList.add(statisticsDaily.getDateCalculated());
                registerNumList.add(statisticsDaily.getRegisterNum());
                loginNumList.add(statisticsDaily.getLoginNum());
                videoViewNumList.add(statisticsDaily.getVideoViewNum());
                courseNumList.add(statisticsDaily.getCourseNum());
            }
        }

        Map<String, Object> result = new HashMap<>();
        result.put("dateCalculateList", dateCalculateList);
        result.put("registerNumList", registerNumList);
        result.put("loginNumList", loginNumList);
        result.put("videoViewNumList", videoViewNumList);
        result.put("courseNumList", courseNumList);
        return result;
    }
}
